import scrapy

from ..items import DyttItem


class DySpider(scrapy.Spider):
    name = "dy"
    allowed_domains = ["dy2018.com"]
    start_urls = ["https://dy2018.com/html/bikan/index.html"]

    def parse(self, response):
        # 要第一页的名字和第二页的图片
        a_list = response.xpath('//div[@class="co_content8"]//td[2]//a[2]')
        for a in a_list:
            name = a.xpath('./text()').extract_first()
            href = a.xpath('./@href').extract_first()
            # 第二页的地址
            url = 'https://dy2018.com' + href
            print(name, url)
            # 对第二页的地址进行访问
            yield scrapy.Request(url=url, callback=self.parse_second, meta={'name': name})

    def parse_second(self, response):
        print('1212')
        image_url = response.xpath('//div[@id="Zoom"]//img[1]/@src').extract_first()
        name_ = response.meta['name']
        print(image_url, name_)
        yield DyttItem(name=name_, url=image_url)
